package com.lz.common.utils.file.replace;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public class WordReplaceUtil {

    public static void main(String[] args) {
        File file = new File("E:\\resource\\layout\\LZ-Md\\技术栈\\spring02.md");
        String old = "C:\\Users\\12875\\Documents\\LZ-Md";
        String newStr = "E:\\resource\\layout\\LZ-Md";
        replace(file, old, newStr);
    }


    public static void replace(File file, String oldStr, String newStr) {
        try {
            // 读取文件内容为字符串
            String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);

            // 替换旧字符串为新字符串
            String updatedContent = content.replace(oldStr, newStr);

            // 将更新后的内容写回文件
            Files.write(file.toPath(), updatedContent.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
